package com.llbnk.springcoding.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.llbnk.springcoding.thread.ThreadEnum;
import com.llbnk.springcoding.thread.ThreadExcutorException;
import com.llbnk.springcoding.thread.ThreadExcutorFactory;
import com.llbnk.springcoding.thread.ThreadExcutorWrapper;
import com.llbnk.springcoding.thread.rejectedPolicy.RejectedPolicyEnum;
import lombok.Getter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sound.midi.Track;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

@Component
@Slf4j
public class TestExcutor {

    public static final String TEST_EXCUTOR_NAME = "test.excutor";

    @Getter
    private ThreadExcutorWrapper taskExecutor;

    public TestExcutor() throws ThreadExcutorException {
        taskExecutor = ThreadExcutorFactory.build(
                (ThreadEnum.ThreadExcutorName.THREAD_POOL_NAME_PREFIX.getValue().concat(TEST_EXCUTOR_NAME)),
                50,
                100,
                3L,
                2000,
                true,
                true,
                3L,
                RejectedPolicyEnum.ABORT_POLICY
                );
    }

    @PreDestroy
    public void shutDownTaskPool() {
        taskExecutor.shutdown();
    }
    /**
     * 每个分钟的第 30 秒采集一次
     */
    @Scheduled(cron = "30 * * * * ?")
    public void traceThreadPoolStatus() {
        if (taskExecutor != null) {
            int active = ((ThreadPoolExecutor)taskExecutor.getExecutorService()).getActiveCount();
            int queueSize = ((ThreadPoolExecutor)taskExecutor.getExecutorService()).getQueue().size();
            log.info("[TestExcutor] TestExcutor is active:{},queueSize:{}", active, queueSize);
        } else {
            log.warn("[TestExcutor] TestExcutor has not been initialized");
        }
    }
}



//@Component
//@Slf4j
//public class TestExcutor {
//
//    @Getter
//    private ThreadPoolTaskExecutor taskExecutor;
//
//    @Value("${excutor.test.corePoolSize:50}")
//    private int corePoolSize;
//    @Value("${excutor.test.queueCapacity:100}")
//    private int queueCapacity;
//    @Value("${excutor.test.maxPoolSize:100}")
//    private int maxPoolSize;
//    @Value("${excutor.test.awaitTerminationSeconds:3}")
//    private int awaitTerminationSeconds;
//    public TestExcutor() {
//        taskExecutor = new ThreadPoolTaskExecutor();
//    }
//
//    @PostConstruct()
//    private void initTestExecutor() {
//        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("test-Excutor-%d").build();
//        taskExecutor.setThreadFactory(namedThreadFactory);
//        taskExecutor.setCorePoolSize(corePoolSize);
//        taskExecutor.setQueueCapacity(queueCapacity);
//        taskExecutor.setMaxPoolSize(maxPoolSize);
//        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
//        taskExecutor.setAwaitTerminationSeconds(awaitTerminationSeconds);
//        taskExecutor.initialize();
//    }
//    @PreDestroy
//    public void shutDownTaskPool() {
//        taskExecutor.shutdown();
//    }
//    /**
//     * 每个分钟的第 30 秒采集一次
//     */
//    @Scheduled(cron = "30 * * * * ?")
//    public void traceThreadPoolStatus() {
//        if (taskExecutor != null) {
//            int active = taskExecutor.getActiveCount();
//            int queueSize = taskExecutor.getThreadPoolExecutor().getQueue().size();
//            log.info("[TestExcutor] TestExcutor is active:{},queueSize:{}", active, queueSize);
//        } else {
//            log.warn("[TestExcutor] TestExcutor has not been initialized");
//        }
//    }
//}